<?php
class DefaultInstall5Action extends InstallAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'index';
	}
	
	public function execute()
	{
		
	}
	
	protected function afterExecute()
	{
		set_time_limit(0);
		$name 		= htmlspecialchars($this->context->get('name'));
		$password 	= htmlspecialchars($this->context->get('password'));
		$code 		= htmlspecialchars($this->context->get('code'));
		
		if($name=='' || $password=='')
		{
			header('Location:'.C('LtUrl')->generate('Default','Install4'));
		}
		
		if($code=='')
		{
			$code		= C('CommonFunction')->random(2);
		}
		$confBaseDir	= substr(dirname(__FILE__), 0, strrpos(__FILE__, "code")).'install/';
		
		$fp 	= fopen($confBaseDir.'pisces.sql', 'r');
		$sql 	= fread($fp, filesize($confBaseDir.'pisces.sql'));
		fclose($fp);
		
		$is				= new InstallService();
		$is->runSql(C('LtConfig')->get('db.table.prefix'), $sql);
		
		$secques		= intval(rand(10000,100000));
		$adminService	= new AdminService();
		$admin			= array('username'=>$name,'password'=>md5($password.$secques),'secques'=>$secques);
		$id				= $adminService->add($admin);
		if($id<=0)
		{
			die ('添加管理员帐号出错');
		}
		//初始化设置
		$settingService	= new SettingService();
		$data	= array();
		$data['pisces_api_url'] 		= 'http://ad.taozhangmen.net/api.php?module=Api&action=Index';
		$data['taobao_api_url'] 		= 'http://gw.api.taobao.com/router/rest';
		$data['taobao_api_userlogin'] 	= 'http://container.open.taobao.com/container?appkey=';
		$data["taobao_api_appkey"] 		= "输入你在淘宝开放平台申请的APP KEY";
		$data["taobao_api_appsecret"] 	= "输入你在淘宝开放平台的APP SECRET";
		$data["taobao_api_nick"] 		= "输入你的淘宝账号";
		$data["taobao_api_pid"] 		= "输入你的阿里妈妈PID";
		$data["taobao_api_num"] 		= "1";
		$data["format"] 				= "json";
		$settingService->add('api_setting',$data,true);
		$data	= array();
		$data['template'] 	= 'tzm';
		$data['id_key'] 	= $code;
		$data["site_name"] 	= "淘掌门";
		$data["beian"] 		= "你的备案号";
		$data["email"] 		= "你的Email";
		$data["qq"] 		= "你的QQ";
		$data["wangwang"] 	= "你的旺旺";
		$data["tongji"] 	= "你的统计代码";
		$settingService->add('site_setting',$data,true);
		$data	= array();
		$data['system#encode'] 		= 'GB2312';
		$data['system#ipcheck']		= 0;
		$data["system#autoadmit"]	= 0;
		$data["search8#enable"]		= 0;
		$data["search8#keyword"]	= "";
		$data["search_key#enable"]	= 1;
		$settingService->add('config_setting',$data,true);
		$data	= array('google'=>1,'msn'=>1,'yahoo'=>1,'yodao'=>1,'iask'=>1,'sogou'=>1,'soso'=>1,'other'=>1,);
		$settingService->add('spider_setting',$data,true);
		$data	= array();
		$data["mode"]					= "path_info";
		$data["postfix"]				= ".html";
		$data["listshortcut"]			= "list";
		$data["detailshortcut"]			= "view";
		$data["postlistshortcut"]		= "forum";
		$data["postdetailshortcut"]		= "thread";
		$data["pindaoindexshortcut"]	= "pindao";
		$data["pageindexshortcut"]		= "zhuanti";
		$data["indextitle"] 		= "淘掌门淘宝客,免费的淘宝客程序,淘宝返利,淘宝返现购物";
		$data["indexkeyword"] 		= "淘掌门淘宝客|免费的淘宝客程序|淘宝返利|淘宝返现购物";
		$data["indexdescription"] 	= "免费下载淘掌门淘宝客源程序";
		$data["listtitle"] 			= "淘宝网热卖{catName}{keyword}{page}-淘掌门淘宝客";
		$data["listkeyword"] 		= "淘宝网热卖|{catName}";
		$data["listdescription"] 	= "淘宝网2010年热卖的{catName}，尽在淘掌门淘宝客";
		$data["detailtitle"] 		= "淘宝网热卖{title}-淘掌门淘宝客";
		$data["detailkeyword"] 		= "{title}";
		$data["detaildescription"] 	= "淘宝网2010年热卖的{title}，尽在淘掌门淘宝客";
		$settingService->add('seo_setting',$data,true);
		
		$data	= array();
		$data['page_size']	= 40;
		$settingService->add('list_setting',$data,true);
		
		$data	= array();
		$data['cid']		= 16;
		$data['keyword']	= '女装,精品女装';
		$settingService->add('list_relate_setting',$data,true);
		
		$data	= array();
		$data['nick']	= 'kiss寒心';
		$settingService->add('click_count_setting',$data,true);
		
		$adService		= new AdService();
		$adService->del(-1);
		
		$opts = array(
		    'http'=>array(
		    'method'=>"GET",
		    'timeout'=>3,
		  )
		);
		$context = stream_context_create($opts);
		
		$links 	= @file_get_contents('http://ad.taozhangmen.net/api.php?module=Link&action=Index',false,$context);
		if($links)
		{
			$tmp	= json_decode($links,true);
			if(!empty($tmp['data']['data']))
			{
				$linkService = new LinkService();
				foreach($tmp['data']['data'] as $l)
				{
					$linkService->add($l);
				}
			}
		}
		
		//清除临时文件
		$tmpBaseDir	= substr(dirname(__FILE__), 0, strrpos(__FILE__, "app")).'tmp/';
		unlink($tmpBaseDir.'test/test/write_test.txt');
		rmdir($tmpBaseDir.'test/test/');
		rmdir($tmpBaseDir.'test/');
		
		//生成lock文件
		$fp 		= @fopen($tmpBaseDir."/install.lock", 'w');
		fwrite($fp,date('Y-m-d H:is'));
		fclose($fp);
		
		//安装日志
		@file_get_contents('http://ad.taozhangmen.net/api.php?module=Install&action=Log&url='.urlencode($this->data['domain'].$this->data['baseurl']),false,$context);
		
		echo '<script type="text/javascript">document.getElementById("btnnext").disabled = false; </script>'."\r\n";
		echo '<script type="text/javascript">document.getElementById("btnnext").value = \'安装完毕\'; </script>'."\r\n";
	}
}
